package com.bootdo.config;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.StringUtils;
/*import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;*/
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * 全文检索工具类
 * @author  郝江波
 *
 */
//@Component
public class EsUtils {
	/*@Autowired
	private TransportClient transportClient;


	public TransportClient getTransportClient() {
		return transportClient;
	}

	public void setTransportClient(TransportClient transportClient) {
		this.transportClient = transportClient;
	}

	*//**
	 * 保存
	 * @param index
	 * @param type
	 * @param id
	 *//*
	public void saveObject(String index,String type,String id,JSONObject jsonObject){
		transportClient.prepareIndex(index, type, id).setSource(jsonObject,XContentType.JSON).get();
	}
	*//**
	 * 更新
	 * @param index
	 * @param type
	 * @param id
	 *//*
	public void updateObject(String index,String type,String id,JSONObject jsonObject){
		transportClient.prepareUpdate(index, type, id).setDoc(jsonObject).get();
	}
	*//**
	 * 删除
	 * @param index
	 * @param type
	 * @param id
	 *//*
	public void deleteObject(String index,String type,String id){
		transportClient.prepareDelete(index, type, id).get();
	}
	
	*//**
	 * 根据ID查询
	 * @param index
	 * @param type
	 * @param id
	 * @return
	 *//*
	public JSONObject getObjectById(String index,String type,String id){
		
		String result = transportClient.prepareGet(index, type, id).get().getSourceAsString();
		if(StringUtils.isEmpty(result))
			return null;
		else
			return JSONObject.parseObject(result);
	}
	
	*//**
	 * 分词检索
	 * @param index
	 * @param type
	 * @param key
	 * @param value

	 * @return
	 *//*
	public Page getObjectByMatch(String index,String type,String key,String value,Page page){
		 List<JSONObject> l = new ArrayList<JSONObject>();
		 SearchResponse response = transportClient.prepareSearch(index).setTypes(type)
		.setQuery(QueryBuilders.matchQuery(key, value))
		.setFrom(page.getPageNo()).setSize(page.getPageSize()).setExplain(true)
        .get();	 
		 if(response.getHits().getTotalHits() > 0){
			 for(SearchHit s : response.getHits()){
				 page.getList().add(JSONObject.parseObject(s.getSourceAsString()));
			 }
			 page.setCount(response.getHits().getTotalHits());
			 return page;
		  }else{
			  return null;
		  }

	}


	*//**
	 * 分词检索  不分页
	 * @param index
	 * @param type
	 * @param key
	 * @param value

	 * @return
	 *//*
	public List<JSONObject> getObjectByMatch(String index,String type,String key,String value){
		List<JSONObject> l = new ArrayList<JSONObject>();
		SearchResponse response = transportClient.prepareSearch(index).setTypes(type)
				.setQuery(QueryBuilders.matchQuery(key, value))
				.get();
		if(response.getHits().getTotalHits() > 0){
			for(SearchHit s : response.getHits()){
				l.add(JSONObject.parseObject(s.getSourceAsString()));
			}
			return l;
		}else{
			return null;
		}

	}


	*//**
	 * 模糊检索  英文或者数字
	 * @param index
	 * @param type
	 * @param key
	 * @param value

	 * @return
	 *//*
	public List<JSONObject> getObjectByWildcardQuery(String index,String type,String key,String value){
		List<JSONObject> l = new ArrayList<JSONObject>();
		SearchResponse response = transportClient.prepareSearch(index).setTypes(type)
				.setQuery(QueryBuilders.wildcardQuery(key, "*"+value+"*"))
				.get();
		if(response.getHits().getTotalHits() > 0){
			for(SearchHit s : response.getHits()){
				l.add(JSONObject.parseObject(s.getSourceAsString()));
			}
			return l;
		}else{
			return null;
		}

	}

	*//**
	 * 中文检索不分词 不分页
	 * @param index
	 * @param type
	 * @param key
	 * @param value
	 * @return
	 *//*
	public List<JSONObject> getObjectByMatchPhraseQuery(String index,String type,String key,String value){
		List<JSONObject> l = new ArrayList<JSONObject>();
		SearchResponse response = transportClient.prepareSearch(index).setTypes(type)
				.setQuery(QueryBuilders.matchPhraseQuery(key,  value))
				.get();
		if(response.getHits().getTotalHits() > 0){
			for(SearchHit s : response.getHits()){
				l.add(JSONObject.parseObject(s.getSourceAsString()));
			}
			return l;
		}else{
			return null;
		}

	}


	*//**
	 * 中文检索不分词 分页
	 * @param index
	 * @param type
	 * @param key
	 * @param value
	 * @return
	 *//*
	public Page getObjectByMatchPhraseQuery(String index,String type,String key,String value,Page page){

		HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
		highlightBuilder.preTags("<span style=\"color:red\">");
		highlightBuilder.postTags("</span>");
		SearchResponse response = transportClient.prepareSearch(index).setTypes(type)
				.setQuery(QueryBuilders.matchPhraseQuery(key,  value))
				.highlighter(highlightBuilder)
				.setFrom(page.getPageNo()).setSize(page.getPageSize()).setExplain(true).get();
				*//*.setFrom(0)
				.setSize(5)
				.setExplain(true)
				.execute().actionGet();*//*
		System.err.println(response.toString());
		if(response.getHits().getTotalHits() > 0){
			page.setCount(response.getHits().getTotalHits());
			for(SearchHit s : response.getHits()){
				page.getList().add(JSONObject.parseObject(s.getSourceAsString()));
			}

			page.setCount(response.getHits().getTotalHits());

			return page;
		}else{
			return null;
		}

	}


	@Test
 	public  void  testEs() throws UnknownHostException {
	 TransportClient t = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddresses(
			 new InetSocketTransportAddress(InetAddress.getByName("songba.hbweiyinqing.cn"),9300));
		 EsUtils e = new EsUtils();
		 e.setTransportClient(t);

		 String index = "memberindex";
		 String type = "member";

		 JSONObject jsonObject = new JSONObject();

		// e.saveObject(index,type,);


	}


	*//**
	 * 内部分页工具
	 * @author 郝江波
	 *
	 * @param <T>
	 *//*
	public static 	class Page<T>{
		int pageNo;
		int pageSize;

		List<T> list = new ArrayList<T>();

		public Page(int pageNo,int pageSize){
			EsUtils.Page.this.pageNo = pageNo;
			EsUtils.Page.this.pageSize = pageSize;
		}
		long count;
		public int getPageNo() {
			return pageNo;
		}
		public void setPageNo(int pageNo) {
			this.pageNo = pageNo;
		}
		public int getPageSize() {
			return pageSize;
		}
		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}
		public List<T> getList() {
			return list;
		}
		public long getCount() {
			return count;
		}
		public void setCount(long count) {
			this.count = count;
		}
	}
	*/
}
